<template>
  <el-dialog :title="info.title" :visible.sync="visible" center width="460px" :before-close="handleCancel">
    <el-form ref="formDate" label-suffix=":" label-width="100px" :rules="rules" :model="form">
      <el-row>
        <el-col :span="24">
          <el-form-item style="width: 100%" label="指定项目" prop="pro_id">
            <el-select
              v-model="form.pro_id"
              size="small"
              :remote-method="selPros"
              remote
              filterable
              style="width: 100%"
              placeholder="请选择项目名称"
              clearable
              @focus="selPros"
            >
              <el-option
                v-for="item in pors"
                :key="item.key"
                :label="item.value"
                :value="item.key"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="fr mt10">
            <el-button size="small" @click="handleCancel()">取 消</el-button>
            <el-button type="primary" size="small" @click="handleIn">采 购</el-button>
          </div>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import { selPros } from '@/api/project'
import { purTo } from '@/api/purchase'

export default {
  props: {
    info: {
      type: Object,
      default: null
    },
    getData: {
      type: Function,
      default: function() {}
    }
  },
  data() {
    return {
      form: {},
      pors: [],
      rules: {
        pro_id: [{ required: true, trigger: 'blur', message: '指定项目不可为空' }]
      },
      visible: false
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.$set(this, 'pors', [])
        this.form = this.info.edit
      }
    },
    getData: {
      type: Function,
      default: function() {}
    }
  },
  mounted() {
  },
  methods: {
    selPros(query) {
      let str = ''
      if (Object.prototype.toString.call(query) === '[object String]') str = query
      selPros({ name: str }).then(response => {
        this.pors = response.pros
      }).catch(err => {
        this.$message.error(err)
      })
    },
    handleIn() {
      this.$refs['formDate'].validate(async(valid) => {
        if (valid) {
          this.$confirm('确定已采购了么?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(async() => {
            purTo(this.form).then(response => {
              this.getData()
              this.handleCancel()
              this.$message.success('采购成功')
            }).catch(error => {
              console.log(error)
              this.$message.error(error.msg ? error.msg : '采购失败')
            })
          })
        }
      })
    },
    handleCancel(done) {
      if (this.$refs['formDate']) this.$refs['formDate'].resetFields()
      if (done && typeof done === Function) {
        done()
      } else {
        this.visible = false
      }
    }
  }
}
</script>

<style>

</style>
